﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exercise1_2
{
    public class Student
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }

    static class Extensions
    {
        //-----------------1
        public static StringBuilder Substring(this StringBuilder builder, int index, int length)
        {
            return new StringBuilder(builder.ToString().Substring(index, length));
        }
       //------------------

        //-----------------2
        public static T Min<T>(this IEnumerable<T> collection)where T : IComparable<T>
        {
            T minItem = default(T);
            foreach(T item in collection)
            {
             minItem= item;
                break;
            }
            foreach (T item in collection)
            { 
                if(item.CompareTo(minItem)==-1)
                {
                    minItem=item;
                }
            }
            return minItem;
        }

        public static T Max<T>(this IEnumerable<T> collection) where T : IComparable<T>
        {
            T maxItem = default(T);
            foreach (T item in collection)
            {
                maxItem = item;
                break;
            }
            foreach (T item in collection)
            {
                if (item.CompareTo(maxItem) == 1)
                {
                    maxItem = item;
                }
            }
            return maxItem;
        }
        //-----------------

        //-----------------3
        public static IEnumerable<Student> FirstNameBeforeLastName(Student[] students)
        {
            return students.Where(stud => stud.FirstName.CompareTo(stud.LastName) == -1);
        }
        //-----------------

        //-----------------4
        public static IEnumerable<Student> AllStudentsBetween18And24Years(Student[] students)
        {
            return students.Where(stud => stud.Age >= 18 && stud.Age <= 24);
        }
        //-----------------

        //-----------------5
        public static IEnumerable<Student> SortByNames(Student[] students)
        {
            return students.OrderBy(stud => stud.FirstName).ThenBy(stud => stud.LastName);
        }
        public static IEnumerable<Student> SortByNamesLINQ(Student[] students)
        {
            var allStudents = from stud in students orderby stud.FirstName, stud.LastName select stud;
                              return allStudents;
        }
        //-----------------

        //-----------------6

        //-----------------
    }
    public class Test
    {
        public static void Main()
        {
            List<int> arr = new List<int>(new int[] { 1, 2, 3, 4 });
            Console.WriteLine(arr.Max());
        }
    }
}
